// 单点登录核心类
import { context } from '@pkg/request/bridge';
import { getUrlParam } from '@pkg/utils';
import { validateCasLogin } from '@pkg/apis/sys';
import { useUserStore } from '@/store/user';

const openSso = true;

export function useSso() {
  /**
   * 单点登录
   */
  async function ssoLogin() {
    if (openSso == true) {
      const token = context.getTokenFunction?.();
      let account = getUrlParam('account');
      let tempCode = getUrlParam('tempCode');
      let sign = getUrlParam('sign');
      if (!token) {
        if (account) {
          await validateCasLogin({
            account: account,
            tempCode: tempCode,
            sign: sign,
          }).then((res) => {
            const userStore = useUserStore();
            userStore.setToken(res);
            return userStore.afterLoginAction(true, {});
          });
        }
      }
    }
  }

  return { ssoLogin };
}
